package com.example.easeimpro.model.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import androidx.annotation.NonNull;

import com.example.easeimpro.model.bean.GroupInfo;
import com.example.easeimpro.model.bean.InviteInfo;
import com.example.easeimpro.model.bean.UserInfo;
import com.example.easeimpro.model.db.DBHelper;

import java.util.ArrayList;
import java.util.List;

public class InviteTableDao {
        private DBHelper mHelper;
        private SQLiteDatabase db;
        public InviteTableDao(DBHelper helper){
                mHelper = helper;

        }

        private static final String TAG = "InviteTableDao";
        //添加邀请
        public void addInvitation(@NonNull InviteInfo inviteInfo){
                getInitDB();
               // Log.e(TAG, "addInvitation: "+inviteInfo.getReason() );
                ContentValues values = new ContentValues();
                values.put(InviteTable.COL_REASON,inviteInfo.getReason());//原因
                values.put(InviteTable.COL_STATUS,inviteInfo.getStatus().ordinal());//状态
                UserInfo userInfo = inviteInfo.getUser();
                if(userInfo != null){
                        //联系人
                        values.put(InviteTable.COL_USER_HXID,inviteInfo.getUser().getHxId());
                        values.put(InviteTable.COL_USER_NAME,inviteInfo.getUser().getName());
                }else {
                        values.put(InviteTable.COL_GROUP_HXID,inviteInfo.getGroup().getGroupId());
                        values.put(InviteTable.COL_GROUP_NAME,inviteInfo.getGroup().getGroupName());
                        values.put(InviteTable.COL_USER_HXID,inviteInfo.getGroup().getInvatePerson());
                        //群组
                }
                long insert = db.insert(InviteTable.TAB_NAME, null, values);

              //  Log.e(TAG, "addInvitation: "+insert );
        }
        //获取所有邀请信息
        @SuppressLint("Range")
        public List<InviteInfo> getInvites(){
                getInitDB();
                String sql = "select * from "+InviteTable.TAB_NAME;

                Cursor cursor = db.rawQuery(sql, null);//执行语句
                List<InviteInfo> list = new ArrayList<>();
                while (cursor.moveToNext()){
                        InviteInfo info = new InviteInfo();
                        info.setReason(cursor.getString(cursor.getColumnIndex(InviteTable.COL_REASON)));
                       info.setStatus(intInviteStatus(cursor.getInt(cursor.getColumnIndex(InviteTable.COL_STATUS))));
                        String group = cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_HXID));
                        if(group == null){
                                //是联系人邀请信息
                                UserInfo userInfo  = new UserInfo();
                                userInfo.setHxid(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_HXID)));
                                userInfo.setName(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_NAME)));
                                userInfo.setNick(cursor.getString(cursor.getColumnIndex(InviteTable.COL_USER_NAME)));
                                info.setUser(userInfo);

                        }else{
                                //群组的邀请信息
                                GroupInfo groupInfo = new GroupInfo();
                                groupInfo.setGroupId(cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_HXID)));
                                groupInfo.setGroupName(cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_NAME)));
                                groupInfo.setInvatePerson(cursor.getString(cursor.getColumnIndex(InviteTable.COL_GROUP_HXID)));
                                info.setGroup(groupInfo);
                        }
                        list.add(info);
                }


                //关闭资源z
                cursor.close();
                //返回联系人列表
                return list;

        }

        @SuppressLint("Range")
        public InviteInfo.InvationStatus intInviteStatus(int intStatus){
                if(intStatus == InviteInfo.InvationStatus.NEW_INVITE.ordinal()){
                        return InviteInfo.InvationStatus.NEW_INVITE;
                }
                if(intStatus == InviteInfo.InvationStatus.INVITE_ACCEPT.ordinal()){
                        return InviteInfo.InvationStatus.INVITE_ACCEPT;
                }
                if(intStatus == InviteInfo.InvationStatus.NEW_GROUP_INVITE.ordinal()){
                        return InviteInfo.InvationStatus.NEW_GROUP_INVITE;
                }
                if(intStatus == InviteInfo.InvationStatus.NEW_GROUP_APPLICATION.ordinal()){
                        return InviteInfo.InvationStatus.NEW_GROUP_APPLICATION;
                }
                if(intStatus == InviteInfo.InvationStatus.GROUP_INVITE_ACCEPTED.ordinal()){
                        return InviteInfo.InvationStatus.GROUP_INVITE_ACCEPTED;
                }
                if(intStatus == InviteInfo.InvationStatus.GROUP_APPLICATION_ACCEPTED.ordinal()){
                        return InviteInfo.InvationStatus.GROUP_APPLICATION_ACCEPTED;
                }
                if(intStatus == InviteInfo.InvationStatus.GROUP_ACCEPT_INVITE.ordinal()){
                        return InviteInfo.InvationStatus.GROUP_ACCEPT_INVITE;
                }
                if(intStatus == InviteInfo.InvationStatus.GROUP_ACCEPT_APPLICATION.ordinal()){
                        return InviteInfo.InvationStatus.GROUP_ACCEPT_APPLICATION;
                }
                if(intStatus == InviteInfo.InvationStatus.GROUP_REJECT_INVITE.ordinal()){
                        return InviteInfo.InvationStatus.GROUP_REJECT_INVITE;
                }
                if(intStatus == InviteInfo.InvationStatus.GROUP_REJECT_APPLICATION.ordinal()){
                        return InviteInfo.InvationStatus.GROUP_REJECT_APPLICATION;
                }
                if(intStatus == InviteInfo.InvationStatus.GROUP_INVITE_DECLINED.ordinal()){
                        return InviteInfo.InvationStatus.GROUP_INVITE_DECLINED;
                }
                if(intStatus == InviteInfo.InvationStatus.GROUP_APPLICATION_DECLINED.ordinal()){
                        return InviteInfo.InvationStatus.GROUP_APPLICATION_DECLINED;
                }


                return null;
        }



        //更新邀请信息
        public void updateInviteStatus(InviteInfo.InvationStatus invationStatus,String hxId){
                getInitDB();
                if(hxId == null ){
                        return;
                }
                //执行语句
                ContentValues values = new ContentValues();
                values.put(InviteTable.COL_STATUS,invationStatus.ordinal());

                int update = db.update(InviteTable.TAB_NAME, values, InviteTable.COL_USER_HXID + " = ?", new String[]{hxId});


        }
        public void updateInviteReason(InviteInfo info,String hxId){
                getInitDB();
                if(hxId == null ){
                        return;
                }

                //执行语句
                ContentValues values = new ContentValues();
                values.put(InviteTable.COL_REASON,info.getReason());

                int update = db.update(InviteTable.TAB_NAME, values, InviteTable.COL_USER_HXID + " = ?", new String[]{hxId});


        }
        //删除邀请信息
        public void removeInviteStatus(String hxId){
                getInitDB();
                if(hxId == null ){
                        return;
                }
                int delete = db.delete(InviteTable.TAB_NAME, InviteTable.COL_USER_HXID + " = ?", new String[]{hxId});
                Log.e(TAG, "removeInviteStatus: "+delete );
        }
          public void getInitDB(){
                  db = mHelper.getWritableDatabase();
          }
}
